package com.jic.order.controller.manage;

import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.RestResult;
import com.jic.common.base.vo.enums.DeptTypeEnums;
import com.jic.order.request.OrderDeliveryTypeRequest;
import com.jic.order.response.OrderDeliveryTypeResponse;
import com.jic.common.base.vo.PageResult;
import com.jic.order.service.OrderDeliveryTypeService;
import com.jic.order.utils.BeanUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Api(value = "配送方式管理", tags = {"配送方式管理"})
@RequestMapping(value = "orderDistributionType")
@RestController
public class OrderDistributionTypeController {

    @Autowired
    OrderDeliveryTypeService orderDeliveryTypeService;

    @ApiOperation(value = "配送方式分页查询", notes = "分页查询配送方式")
    @RequestMapping(value = "queryPage", method = RequestMethod.POST)
    public RestResult<PageResult<OrderDeliveryTypeResponse>> queryPage(@RequestBody OrderDeliveryTypeRequest form) {

        Page pageQuery = new Page();
        pageQuery.setLimit(form.getLimit());
        pageQuery.setStart(form.getStart());

        OrderDeliveryTypeRequest orderDeliveryType = BeanUtil.copy(form, OrderDeliveryTypeRequest.class);
        DeptTypeEnums deptType = form.getAdminOperate().getDeptType();
        if (deptType.getCode() != 0) {
            orderDeliveryType.setDeptId(form.getDeptId());
        }
        PageResult<OrderDeliveryTypeResponse> result = orderDeliveryTypeService.queryPage(orderDeliveryType, pageQuery);

        List<OrderDeliveryTypeResponse> orderDeliveryTypeResponseList = result.getRows().stream().map(temp -> {
            OrderDeliveryTypeResponse orderDeliveryTypeResponse = BeanUtil.copy(temp, OrderDeliveryTypeResponse.class);
            return orderDeliveryTypeResponse;
        }).filter(item -> item != null).collect(Collectors.toList());


        PageResult<OrderDeliveryTypeResponse> pageResult = new PageResult<>();
        pageResult.setRows(orderDeliveryTypeResponseList);
        pageResult.setTotal(result.getTotal());
        return RestResult.success(pageResult);
    }

    @ApiOperation(value = "配送方式 禁用/启用", notes = "配送方式 禁用/启用修改")
    @RequestMapping(value = "updateOrderDeliveryType", method = RequestMethod.POST)
    public RestResult updateOrderDeliveryType(@RequestBody OrderDeliveryTypeRequest orderDeliveryType) {
        RestResult restResult = new RestResult<>();
        try {
            //修改
            if (orderDeliveryType.getId() > 0) {
                long flag = orderDeliveryTypeService.update(orderDeliveryType);
                restResult = RestResult.success(restResult);
            } else {
                long flag = orderDeliveryTypeService.insert(orderDeliveryType);
                restResult = RestResult.success(restResult);
            }

        } catch (Exception e) {
            e.printStackTrace();
            restResult = RestResult.error("9999", e.getLocalizedMessage());
        }
        return restResult;
    }

}
